/**
 * 
 */
package com.daogou.jdbc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.daogou.jdbc.connection.JDBCConnection;
import com.daogou.jdbc.model.User;

/**
 * @author jianwen
 *
 */
public class UserDao {
	
	/**
	 * 插入用户
	 * @param u
	 */
	public void insert(User u){
		
		String sql = "insert into t_user values("+
		u.getId()+",'"+
		u.getUsername()
		+"','"+u.getPassword()+"')";
		System.out.println(sql);
		//1,获得数据库连接
		
		JDBCConnection conn = new JDBCConnection();
		try {
			Connection connection = conn.getConection();
			Statement stat = connection.createStatement();
			stat.execute(sql);
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	
	public List<User> searchUser(String name){
		String sql = "select * from t_user where user_name like '%"+name+"%'";
		System.out.println(sql);
		
		 List<User> list = new ArrayList<User>();
		//1,获得数据库连接
		
		JDBCConnection conn = new JDBCConnection();
		try {
			Connection connection = conn.getConection();
			Statement stat = connection.createStatement();
			ResultSet result = stat.executeQuery(sql);
			while(result.next()){
				User u = new User();
				u.setId(result.getInt(1));
				u.setUsername(result.getString(2));
				u.setPassword(result.getString(3));
				list.add(u);
			}
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}

}
